package com.example.project3.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.project3.entity.Projects;
import com.example.project3.entity.User;
import com.example.project3.entity.WorkGroup;
import com.example.project3.exception.ServiceException;
import com.example.project3.mapper.ProjectsMapper;
import com.example.project3.mapper.UserMapper;
import com.example.project3.mapper.WorkGroupMapper;
import com.example.project3.service.ProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 23931
 * @description 针对表【projects】的数据库操作Service实现
 * @createDate 2023-05-29 15:49:00
 */
@Service
public class ProjectsServiceImpl extends ServiceImpl<ProjectsMapper, Projects>
        implements ProjectsService {


    private ProjectsMapper projectsMapper;
    private UserMapper userMapper;
    private WorkGroupMapper workGroupMapper;

    @Override
    public List<Projects> getMyParticipatingProject(User user) {
        List<Projects> result = projectsMapper.getMyParticipatingProject(user);
        return result;
    }

    @Override
    public User invite(Projects project, User inviteUser) throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("email", inviteUser.getEmail());
        List<User> temp = userMapper.selectByMap(query);
        if (temp.size() != 1) {
            throw new ServiceException("未找到" + inviteUser.getEmail());
        } else {
            inviteUser = temp.get(0);
            WorkGroup save = new WorkGroup();
            save.setProjectId(project.getProjectId());
            save.setUid(inviteUser.getUid());
            workGroupMapper.insert(save);
        }
        inviteUser.setPassword("");
        inviteUser.setIcsFileName("");
        return inviteUser;
    }

    @Override
    public Map<String, Object> getMoreInfo(Integer projectId) {
        return projectsMapper.getMoreInfo(projectId);
    }

    @Autowired
    public void setProjectsMapper(ProjectsMapper projectsMapper) {
        this.projectsMapper = projectsMapper;
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setWorkGroupMapper(WorkGroupMapper workGroupMapper) {
        this.workGroupMapper = workGroupMapper;
    }
}




